<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="guide" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hshowADD('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          default-expand-all
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hshowADD(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hshowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      title="添加"
      :visible.sync="showdialog"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <dept-dialog v-if="showdialog" :id="curID" :edit="isEdit" :originer-list="originerList" @success="hsuccess" @close="hclose" />
    </el-dialog>
    <!-- 当封装相同子组件的时候，很容易产生数据残留的bug，原因是dialog隐藏的时候没有销毁子组件，会有数据残留 -->
    <el-dialog
      title="编辑"
      :visible.sync="showdialogEdit"
      width="50%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <dept-dialog v-if="showdialogEdit" :id="curID" :edit="isEdit" :originer-list="originerList" @success="hsuccess" @close="hclose" />
    </el-dialog>
  </div>
</template>
<script>
import { delDepartmentById, getDepartmentList } from '@/api/departments.js'
import { tranListToTreeData } from '@/utils/index'
import deptDialog from '@/views/departments/deptDialog'
export default {
  components: { deptDialog },
  data() {
    return {
      // 依赖一份树形数据
      list: [
      ],
      showdialog: false,
      curID: '',
      isEdit: false,
      showdialogEdit: false,
      originerList: []

    }
  },
  created() {
    this.loaddepartmentList()
  },
  methods: {
    // 点击删除按钮，删除数据
    del(id) {
      this.$confirm('您确定要删除吗?嘤嘤嘤', '提示', { type: 'warning' })
        .then(async() => {
          try {
            const res = await delDepartmentById(id)
            this.$message.success(res.message)
            this.loaddepartmentList()
          } catch (e) {
            this.$message.error(e.message)
          }
        })
        .catch(() => {})
    },
    async loaddepartmentList() {
      try {
        const res = await getDepartmentList()
        // console.log(res)
        res.data.depts.shift()
        // 获取源数据
        // 对象解构
        // 把源数组传给子组件，在子组件做检验
        this.originerList = res.data.depts.map(({ code, name, id, pid }) => ({ code, name, id, pid }))
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        console.log(e)
      }
    },
    hshowADD(id) {
      this.showdialog = true
      // 在父组件获取到的id传递到子组件中
      this.curID = id
      this.isEdit = false
    },
    // 点击编辑事件的时候，传id
    // 并且绑定的编辑事件为true
    hshowEdit(id) {
      // 创建一个编辑的dialog，点击的时候，弹框出现
      // 弹出来后，在子组件中发送i请求，处理需求。需要传值
      // 在组件中做父传值处理
      this.showdialogEdit = true
      this.curID = id
      this.isEdit = true
    },
    // 从子组件传过来的事件，做业务处理
    hsuccess() {
      // 子组件传过来的事件，在父组件中调用
      // 添加成功后，关闭dialog
      // 并且重新获取员工信息
      this.showdialog = false
      this.showdialogEdit = false
      this.loaddepartmentList()
    },
    // 点击弹框的取消按钮，关闭弹框
    hclose() {
      this.showdialog = false
      this.showdialogEdit = false
    }
  }
}
</script>
